package com.action;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;
import com.service.UserService;

import com.entity.Customer;
import com.entity.Address;

public class UserAction {
	private Customer loginUser;
	private Address address;

	// private Set addresses = new HashSet(0);

	public UserAction() {
		//System.out.println("UserAction() is called!");
	}

	public Customer getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(Customer loginUser) {
		System.out.println("setLoginUser() is called!");
		System.out.println("LoginUserId:" + loginUser.getCustomerId());
		this.loginUser = loginUser;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		System.out.println("setAddress() is called!");
		this.address = address;
	}

	/*
	 * public Set getAddresses() { return this.loginUser.getAddresses(); }
	 * 
	 * public void setAddresses(Set addresses) {
	 * System.out.println("action set addresses"); this.addresses = addresses;
	 * this.loginUser.setAddresses(addresses); }
	 */

	public String login() {
		UserService userServ = new UserService();
		if (userServ.login(loginUser)) {
			ActionContext ctx = ActionContext.getContext();
			Map<String, Object> session = (Map) ctx.getSession();
			Map<String, Object> request = (Map) ctx.get("request");
			loginUser = (Customer) request.get("loginUser");
			return "loginsuccess";
		}

		return "loginfail";
	}

	public String register() {
		UserService userServ = new UserService();
		if (userServ.register(loginUser))
			return "registersuccess";
		return "registerfail";
	}

	public String update() {
		UserService userServ = new UserService();
		if (userServ.update(loginUser))
			return "updatesucc";
		return "CRUDfail";
	}

	public String delete() {
		UserService userServ = new UserService();
		if (userServ.delete(loginUser))
			return "deletesucc";
		else
			return "CRUDfail";
	}

	public String addAddr() {
		UserService userServ = new UserService();
		if (userServ.addAddr(loginUser, address))
			return "updatesucc";
		return "CRUDfail";
	}

	public String delAddr() {
		UserService userServ = new UserService();
		System.out.println("loginUserId = " + loginUser.getCustomerId());
		System.out.println("AddressId = " + loginUser.getAddress().getAddressId());
		//System.out.println("Detail:" + loginUser.getAddress().getDetail());
		//System.out.println("Detail:" + address.getDetail());
		address = loginUser.getAddress();
		if (userServ.delAddr(loginUser, address))
			return "updatesucc";
		return "CRUDfail";
	}

}
